/*
 * Copyright 2020 Google LLC. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.mlkit.vision.demo.textdetector;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.Log;

import com.google.mlkit.vision.demo.GraphicOverlay;
import com.google.mlkit.vision.demo.GraphicOverlay.Graphic;
import com.google.mlkit.vision.text.Text;
import com.google.mlkit.vision.text.Text.Element;
import com.google.mlkit.vision.text.Text.Line;
import com.google.mlkit.vision.text.Text.TextBlock;

import java.util.Arrays;

/**
 * Graphic instance for rendering TextBlock position, size, and ID within an associated graphic
 * overlay view.
 */
public class TextGraphic extends Graphic {

    private static final String TAG = "TextGraphic";

    private static final int TEXT_COLOR = Color.BLACK;
    private static final int MARKER_COLOR = Color.WHITE;
    private static final float TEXT_SIZE = 54.0f;
    private static final float STROKE_WIDTH = 4.0f;

    private final Paint rectPaint;
    private final Paint textPaint;
    private final Paint labelPaint;
    private final Text text;

    TextGraphic(GraphicOverlay overlay, Text text) {
        super(overlay);

        this.text = text;

        rectPaint = new Paint();
        rectPaint.setColor(MARKER_COLOR);
        rectPaint.setStyle(Paint.Style.STROKE);
        rectPaint.setStrokeWidth(STROKE_WIDTH);

        textPaint = new Paint();
        textPaint.setColor(TEXT_COLOR);
        textPaint.setTextSize(TEXT_SIZE);

        labelPaint = new Paint();
        labelPaint.setColor(MARKER_COLOR);
        labelPaint.setStyle(Paint.Style.FILL);
        // Redraw the overlay, as this graphic has been added.
        postInvalidate();
    }

    /**
     * Draws the text block annotations for position, size, and raw value on the supplied canvas.
     */
    @Override
    public void draw(Canvas canvas) {
        Log.d(TAG, "Text is: " + text.getText());
        for (TextBlock textBlock : text.getTextBlocks()) {
            // Renders the text at the bottom of the box.
            Log.d(TAG, "TextBlock text is: " + textBlock.getText());
            Log.d(TAG, "TextBlock boundingbox is: " + textBlock.getBoundingBox());
            Log.d(TAG, "TextBlock cornerpoint is: " + Arrays.toString(textBlock.getCornerPoints()));
            for (Line line : textBlock.getLines()) {
                Log.d(TAG, "Line text is: " + line.getText());
                Log.d(TAG, "Line boundingbox is: " + line.getBoundingBox());
                Log.d(TAG, "Line cornerpoint is: " + Arrays.toString(line.getCornerPoints()));
                // Draws the bounding box around the TextBlock.
                RectF rect = new RectF(line.getBoundingBox());
                rect.left = translateX(rect.left);
                rect.top = translateY(rect.top);
                rect.right = translateX(rect.right);
                rect.bottom = translateY(rect.bottom);
                canvas.drawRect(rect, rectPaint);

                float lineHeight = TEXT_SIZE + 2 * STROKE_WIDTH;
                float textWidth = textPaint.measureText(line.getText());
                float left = isImageFlipped() ? rect.right : rect.left;
                canvas.drawRect(
                        left - STROKE_WIDTH,
                        rect.top - lineHeight,
                        left + textWidth + 2 * STROKE_WIDTH,
                        rect.top,
                        labelPaint);
                // Renders the text at the bottom of the box.
                canvas.drawText(line.getText(), left, rect.top - STROKE_WIDTH, textPaint);

                for (Element element : line.getElements()) {
                    Log.d(TAG, "Element text is: " + element.getText());
                    Log.d(TAG, "Element boundingbox is: " + element.getBoundingBox());
                    Log.d(TAG, "Element cornerpoint is: " + Arrays.toString(element.getCornerPoints()));
                    Log.d(TAG, "Element language is: " + element.getRecognizedLanguage());
                }
            }
        }
    }
}
